package com.hyc.myvideo.ui.activity.my.fragment.statistics;

import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.my.statistics.StatisticsCourseAdapter;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.ModelCommissiones;
import com.hyc.myvideo.ui.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的统计 - 提成
 * @author gyb
 * @version 1.0
 * @created 2017/07/20
 */
public class StatisticsCourseFragment extends BaseFragment{
    @BindView(R.id.common_list_id)
    PullToRefreshListView toRefreshListView;
    /**页数*/
    private int page = 1;
    /**是否刷新*/
    private boolean isRefresh = true;
    private StatisticsCourseAdapter adapter;
    private List<ModelCommissiones.Commission> listData= null;
    private  ViewHolder viewHolder = null;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_common_listview;
    }

    @Override
    protected void initView() {
        View headerView = View.inflate(getActivity(), R.layout.activity_adapter_statistics_course_header,null);
        toRefreshListView.getRefreshableView().addHeaderView(headerView);
        listData = new ArrayList<>();
        adapter = new StatisticsCourseAdapter(getContext(), listData);
        toRefreshListView.setAdapter(adapter);
        viewHolder = new ViewHolder(headerView);
        toRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                requestData(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                requestData(false);
            }
        });
    }

    @Override
    protected void initData() {
        requestData(true);
    }

    private void requestData(boolean isRefresh){
        this.isRefresh = isRefresh;
        if(isRefresh){
            page = 1;
        }else{
            page++;
        }
        api.requestCommission(page);
    }

    @Override protected void onNexts(String r, String method) {
        BaseResult<ModelCommissiones> result = JSONObject.parseObject(r, new TypeReference<BaseResult<ModelCommissiones>>(){});
        if(result.isCode()){
            if(isRefresh){
                listData.clear();
            }
            ModelCommissiones commissiones = result.getResult();
            setMoney(commissiones.getInfo());
            listData.addAll(commissiones.getList());
            adapter.updateListView(listData);
        }
        clearedListView();
    }

    @Override protected void onError() {
        clearedListView();
    }

    /***
     * 关闭刷新
     */
    private void clearedListView(){
        if(toRefreshListView.isRefreshing()){
            toRefreshListView.onRefreshComplete();
        }
    }

    /****
     * 展示提成总计
     * @param consume
     */
    private void setMoney(ModelCommissiones.Consume consume){
        /**可提成总额*/
        String sum = String.format(getString(R.string.huc_my_money),new java.text.DecimalFormat("0.00").format(consume.getUser_consume()));
        viewHolder.textViewMoney.setText(sum);
        String total = String.format(getString(R.string.huc_my_money),new java.text.DecimalFormat("0.00").format(consume.getUser_commission()));
        /**累计总提成*/
        viewHolder.textViewTotalMoney.setText(total);
    }



    public  class ViewHolder {
        public ViewHolder() {
        }
        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
        /**可提成总额*/
        @BindView(R.id.statistics_header_t_money)
        TextView textViewMoney;
        /**累计总提成*/
        @BindView(R.id.statistics_header_t_total_money)
        TextView textViewTotalMoney;


    }

}
